package com.live.stamp.base.messageconverter;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by wdcao on 2017/11/23.
 */
public class MyBeanSerializerModifier extends BeanSerializerModifier {

    private JsonSerializer<Object> _nullArrayJsonSerializer = new MyNullIntegerJsonSerializer();

    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
                                                     List<BeanPropertyWriter> beanProperties) {
        // 循环所有的beanPropertyWriter
        for (int i = 0; i < beanProperties.size(); i++) {
            BeanPropertyWriter writer = beanProperties.get(i);
            // 判断字段的类型，如果是date、int、double、bigDecimaltyp、float则注册nullSerializer
            if (isDateType(writer) || isIntegerType(writer) || isDoubleType(writer) || isBigDecimalType(writer) || isFloatType(writer)) {
                //给writer注册一个自己的nullSerializer
                writer.assignNullSerializer(this.defaultNullArrayJsonSerializer());
            }
        }

        return beanProperties;
    }

    /**
     *判断是否为数组类型
     * @param writer
     * @return
     */
    protected boolean isArrayType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.isArray() || clazz.equals(List.class) || clazz.equals(Set.class);

    }

    /**
     *判断是否为数组类型
     * @param writer
     * @return
     */
    protected boolean isDateType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(Date.class);
    }

    /**
     *判断是否为int类型
     * @param writer
     * @return
     */
    protected boolean isIntegerType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(Integer.class);

    }

    /**
     *判断是否为double类型
     * @param writer
     * @return
     */
    protected boolean isDoubleType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(Double.class);

    }

    /**
     *判断是否为BigDecimal类型
     * @param writer
     * @return
     */
    protected boolean isBigDecimalType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(BigDecimal.class);

    }

    /**
     *判断是否为float类型
     * @param writer
     * @return
     */
    protected boolean isFloatType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getPropertyType();
        return clazz.equals(Float.class);

    }

    protected JsonSerializer<Object> defaultNullArrayJsonSerializer() {
        return _nullArrayJsonSerializer;
    }
}
